//
//  ReportBasicInfoCell.m
//  efairy
//
//  Created by 李锋 on 2019/3/13.
//  Copyright © 2019年 East. All rights reserved.
//

#import "ReportBasicInfoCell.h"

@interface ReportBasicInfoCell()<UITextFieldDelegate>
@property(nonatomic, assign) BOOL isSave;
@property(nonatomic,strong) NSString *lastReportTitle;
@end

@implementation ReportBasicInfoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}

-(void)initView{
    
    self.contentView.backgroundColor = ColorFromRGB(0xeff0f4);
    
    UIView *titleView = [[UIView alloc]init];
    titleView.backgroundColor = ColorFromRGB(0x28A8FF);
    [titleView.layer setCornerRadius:6];
    
    _reportTitle = [[UITextField alloc]init];
//    _reportTitle.text = @"";
    _reportTitle.font = [UIFont boldSystemFontOfSize:17.f];
    _reportTitle.textColor = [UIColor whiteColor];
    _editImageView = [[UIImageView alloc]init];
    _editImageView.image = [UIImage imageNamed:@"icon_editor"];
    _editBtn = [[UIButton alloc]init];//编辑按钮
    [_editBtn setTitle:@"编辑" forState:UIControlStateNormal];
    [_editBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _editBtn.titleLabel.font  = [UIFont systemFontOfSize:13.f];
    [_editBtn setImage:[UIImage imageNamed:@"icon_editor"] forState:UIControlStateNormal];
    _editBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);//设置字离图的距离
    [_editBtn addTarget:self action:@selector(onClickEditBtn) forControlEvents:UIControlEventTouchUpInside];

    
    [titleView sd_addSubviews:@[_reportTitle,_editBtn]];
    _reportTitle.sd_layout.heightIs(17).topSpaceToView(titleView, 16.5).leftSpaceToView(titleView, 12).rightSpaceToView(titleView, 70);
//    _editImageView.sd_layout.heightIs(10).widthIs(8.3).rightSpaceToView(titleView, 50).topSpaceToView(titleView, 20);
    _editBtn.sd_layout.heightIs(13).widthIs(45).rightSpaceToView(titleView, 12).topSpaceToView(titleView, 18.5);
    
    
    
    UIView *mContentView = [[UIView alloc]init];
    mContentView.backgroundColor = [UIColor whiteColor];
    
    _reportTimeTitle = [[UILabel alloc]init];
    _reportTimeTitle.text = @"报告时间";
    _reportTimeTitle.font = [UIFont systemFontOfSize:14.f];
    _reportTimeTitle.textColor = ColorFromRGB(0x333333);
    
    _reportTime = [[UILabel alloc]init];//报告时间;
    _reportTime.font = [UIFont systemFontOfSize:14.f];
    _reportTime.textColor = ColorFromRGB(0x666666);
    _reportTime.textAlignment = NSTextAlignmentRight;
    
    _alarmRowsTitle = [[UILabel alloc]init];//报警点位
    _alarmRowsTitle.text = @"报警点位";
    _alarmRowsTitle.font = [UIFont systemFontOfSize:14.f];
    _alarmRowsTitle.textColor = ColorFromRGB(0x333333);
    
    _alarmRows = [[UILabel alloc]init];//报警点位
    _alarmRows.font = [UIFont systemFontOfSize:14.f];
    _alarmRows.textColor = ColorFromRGB(0x333333);
    _alarmRows.textAlignment = NSTextAlignmentRight;
    
    _warningRowsTitle = [[UILabel alloc]init];//预警点位
    _warningRowsTitle.text = @"预警点位";
    _warningRowsTitle.font = [UIFont systemFontOfSize:14.f];
    _warningRowsTitle.textColor = ColorFromRGB(0x333333);
    
    _warningRows = [[UILabel alloc]init];//预警点位
    _warningRows.font = [UIFont systemFontOfSize:14.f];
    _warningRows.textColor = ColorFromRGB(0x333333);
    _warningRows.textAlignment = NSTextAlignmentRight;
    
    _troubleRowsTitle = [[UILabel alloc]init];//故障点位
    _troubleRowsTitle.text = @"故障点位";
    _troubleRowsTitle.font = [UIFont systemFontOfSize:14.f];
    _troubleRowsTitle.textColor = ColorFromRGB(0x333333);
    
    _troubleRows = [[UILabel alloc]init];//故障点位
    _troubleRows.font = [UIFont systemFontOfSize:14.f];
    _troubleRows.textColor = ColorFromRGB(0x333333);
    _troubleRows.textAlignment = NSTextAlignmentRight;
    
    _projectRowsTitle = [[UILabel alloc]init];//项目总数
    _projectRowsTitle.text = @"项目总数";
    _projectRowsTitle.font = [UIFont systemFontOfSize:14.f];
    _projectRowsTitle.textColor = ColorFromRGB(0x333333);
    
    _projectRows = [[UILabel alloc]init];//项目总数
    _projectRows.font = [UIFont systemFontOfSize:14.f];
    _projectRows.textColor = ColorFromRGB(0x666666);
    _projectRows.textAlignment = NSTextAlignmentRight;
    
    _scoreTitle = [[UILabel alloc]init];//评分统计
    _scoreTitle.text = @"评分统计";
    _scoreTitle.font = [UIFont systemFontOfSize:14.f];
    _scoreTitle.textColor = ColorFromRGB(0x333333);
    
    _score = [[UILabel alloc]init];//评分统计
    _score.font = [UIFont systemFontOfSize:14.f];
    _score.textColor = ColorFromRGB(0x666666);
    _score.textAlignment = NSTextAlignmentRight;
    
    [mContentView sd_addSubviews:@[_reportTimeTitle,_reportTime,_alarmRowsTitle,_alarmRows,_warningRowsTitle,_warningRows,
                                   _troubleRowsTitle,_troubleRows,_projectRowsTitle,_projectRows,_scoreTitle,_score]];
    
    _reportTimeTitle.sd_layout.widthIs(60).heightIs(14).leftSpaceToView(mContentView, 12).topSpaceToView(mContentView, 37);
    _reportTime.sd_layout.heightIs(14).leftSpaceToView(_reportTimeTitle, 2).rightSpaceToView(mContentView, 12)
    .topEqualToView(_reportTimeTitle);
    
    _alarmRowsTitle.sd_layout.widthIs(60).heightIs(14).leftSpaceToView(mContentView, 12).topSpaceToView(_reportTimeTitle, 12);
    _alarmRows.sd_layout.heightIs(14).leftSpaceToView(_alarmRowsTitle, 2).rightSpaceToView(mContentView, 12)
    .topEqualToView(_alarmRowsTitle);

    _warningRowsTitle.sd_layout.widthIs(60).heightIs(14).leftSpaceToView(mContentView, 12).topSpaceToView(_alarmRowsTitle, 12);
    _warningRows.sd_layout.heightIs(14).leftSpaceToView(_warningRowsTitle, 2).rightSpaceToView(mContentView, 12)
    .topEqualToView(_warningRowsTitle);

    _troubleRowsTitle.sd_layout.widthIs(60).heightIs(14).leftSpaceToView(mContentView, 12).topSpaceToView(_warningRowsTitle, 12);
    _troubleRows.sd_layout.heightIs(14).leftSpaceToView(_troubleRowsTitle, 2).rightSpaceToView(mContentView, 12)
    .topEqualToView(_troubleRowsTitle);


    _projectRowsTitle.sd_layout.widthIs(60).heightIs(14).leftSpaceToView(mContentView, 12).topSpaceToView(_troubleRowsTitle, 12);
    _projectRows.sd_layout.heightIs(14).leftSpaceToView(_projectRowsTitle, 2).rightSpaceToView(mContentView, 12)
    .topEqualToView(_projectRowsTitle);

    _scoreTitle.sd_layout.widthIs(60).heightIs(14).leftSpaceToView(mContentView, 12).topSpaceToView(_projectRowsTitle, 12);
    _score.sd_layout.heightIs(14).leftSpaceToView(_scoreTitle, 2).rightSpaceToView(mContentView, 12)
    .topEqualToView(_scoreTitle);
    
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = ColorFromRGB(0xe5e5e5);
    
    
    _searchView = [[UIView alloc]init];
    _searchView.backgroundColor = [UIColor whiteColor];
    //搜索栏背景
    UIView *searchBg =  [[UIView alloc]init];
    searchBg.layer.borderWidth=0.0f;
    searchBg.layer.cornerRadius=16.0f;
    searchBg.backgroundColor = ColorFromRGB(0xefefef);
    [_searchView addSubview:searchBg];
    
    //创建左侧搜索图标
    UIImageView *searchIcon=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 15, 15)];
    searchIcon.image=[UIImage imageNamed:@"search_icon"];
    [_searchView addSubview:searchIcon];
    
    //文本输入框
    _searchText = [[UITextField alloc]init];
    _searchText.backgroundColor = ColorFromRGB(0xefefef);
    _searchText.clearButtonMode=UITextFieldViewModeAlways;//只要输入框有内容就出现
    // 设置提示文字
    _searchText.placeholder = @"请输项目名称";
    _searchText.layer.borderWidth=0.0f;
    _searchText.layer.cornerRadius=16.0f;
    _searchText.returnKeyType = UIReturnKeySearch;//变为搜索按钮
    _searchText.delegate = self;//设置代理
    [_searchView addSubview:_searchText];
    
    UILabel *scoreLabel = [[UILabel alloc]init];
    scoreLabel.text = @"评分";
    scoreLabel.textColor = ColorFromRGB(0x333333);
    scoreLabel.font = [UIFont systemFontOfSize:14.f];
    [_searchView addSubview:scoreLabel];
    
    _upImageView = [[UIImageView alloc]init];
    _upImageView.userInteractionEnabled  = YES;//可被点击
    _upImageView.image = [UIImage imageNamed:@"icon_score_ascending"];
    UITapGestureRecognizer *upImageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickUpImageView:)];
    [_upImageView addGestureRecognizer:upImageTap];
    [_searchView addSubview:_upImageView];
    
    _downImageView = [[UIImageView alloc]init];
    _downImageView.userInteractionEnabled  = YES;//可被点击
    _downImageView.image = [UIImage imageNamed:@"icon_score_down_no"];
    UITapGestureRecognizer *downImageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickDownImageView:)];
    [_downImageView addGestureRecognizer:downImageTap];
    [_searchView addSubview:_downImageView];
 
    
    searchBg.sd_layout.heightIs(30)
    .topSpaceToView(_searchView, 5)
    .leftSpaceToView(_searchView, 16)
    .rightSpaceToView(_searchView, 75);
    
    searchIcon.sd_layout.heightIs(15)
    .widthIs(15)
    .topSpaceToView(_searchView, 12.5)
    .leftSpaceToView(_searchView, 32);
    
    _searchText.sd_layout.heightIs(30)
    .topSpaceToView(_searchView, 5)
    .leftSpaceToView(searchIcon, 12)
    .rightSpaceToView(_searchView, 75);
    
    scoreLabel.sd_layout.widthIs(29).heightIs(14).rightSpaceToView(_searchView, 30).topSpaceToView(_searchView, 13);
    _upImageView.sd_layout.widthIs(8).heightIs(4).topSpaceToView(_searchView, 14).rightSpaceToView(_searchView, 16);
    _downImageView.sd_layout.widthIs(8).heightIs(4).topSpaceToView(_upImageView, 4).rightSpaceToView(_searchView, 16);
    
    

    
    [self.contentView sd_addSubviews:@[mContentView,line,_searchView,titleView]];
    mContentView.sd_layout.topSpaceToView(self.contentView, 32).widthIs(SCREEN_WIDTH).heightIs(193);
    line.sd_layout.widthIs(SCREEN_WIDTH).heightIs(1).topSpaceToView(mContentView, 0);
    _searchView.sd_layout.widthIs(SCREEN_WIDTH).heightIs(40).topSpaceToView(line, 0);
    titleView.sd_layout.heightIs(50).leftSpaceToView(self.contentView, 12).rightSpaceToView(self.contentView, 12)
    .topSpaceToView(self.contentView, 8);
    [self setupAutoHeightWithBottomView:_searchView bottomMargin:0];
    
}


#pragma mark -UITextFieldDelegate
//搜索 这个函数是重写UITextFieldDelegate 当点击键盘搜索按钮触发
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
     __weak typeof(self) weakSelf = self;
    if(self.queryByKeywordBlock){
        self.queryByKeywordBlock(self.searchText.text);
    }
    return YES;
}

//// 获得焦点
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
//    return YES;
//}
//
//// 失去焦点
//- (void)textFieldDidEndEditing:(UITextField *)textField{
//}


//颜值搜索栏输入的长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
    if (range.length == 1 && string.length == 0) {
//        return YES;
    }else if (textField.text.length >= 30) {
        if(_searchText == textField){
            self.searchText.text = [textField.text substringToIndex:30];
            if(self.shouldChangeBlock){
                self.shouldChangeBlock(self.searchText.text);
            }
        }else if(_reportTitle == textField){
             self.reportTitle.text = [textField.text substringToIndex:30];
        }
        return NO;
    }
    if(_searchText == textField){
        if(self.shouldChangeBlock){
            self.shouldChangeBlock(self.searchText.text);
        }
    }
    return YES;
}


-(void)onClickEditBtn{
    if(_isSave){
        //保存报表标题
        _isSave = NO;
        if(![_lastReportTitle isEqualToString:_reportTitle.text] || [_reportTitle.text isEqualToString:@""]){
            if(self.saveReportTitleBlock){
                _lastReportTitle = _reportTitle.text;
                self.saveReportTitleBlock(_reportTitle.text);
            }
        }
         _reportTitle.enabled = NO;
        [_editBtn setTitle:@"编辑" forState:UIControlStateNormal];
//        [_reportTitle resignFirstResponder];//取消焦点
        
    }else{
        _isSave = YES;
        _reportTitle.enabled = YES;
        [_reportTitle becomeFirstResponder];//获得焦点
        
        [_editBtn setTitle:@"保存" forState:UIControlStateNormal];
    }
    
}

//升序
-(void)onClickUpImageView:(id)tap{
    _upImageView.image = [UIImage imageNamed:@"icon_score_ascending"];
    _downImageView.image = [UIImage imageNamed:@"icon_score_down_no"];
     __weak typeof(self) weakSelf = self;
    if(weakSelf.scoreQeuryBlock){
        weakSelf.scoreQeuryBlock(0);
    }
}

//降序
-(void)onClickDownImageView:(id)tap{
    _upImageView.image = [UIImage imageNamed:@"icon_score_ascending_no"];
    _downImageView.image = [UIImage imageNamed:@"icon_score_down"];
    __weak typeof(self) weakSelf = self;
    if(weakSelf.scoreQeuryBlock){
        weakSelf.scoreQeuryBlock(1);
    }
}

-(void) setDataModel:(ReportBasicModel *)data keyword:(nonnull NSString *)keyword order:(int)order{
    if(order == 0){
        _upImageView.image = [UIImage imageNamed:@"icon_score_ascending"];
        _downImageView.image = [UIImage imageNamed:@"icon_score_down_no"];
    }else{
        _upImageView.image = [UIImage imageNamed:@"icon_score_ascending_no"];
        _downImageView.image = [UIImage imageNamed:@"icon_score_down"];
    }
     _searchText.text = keyword;
    if(data){
        if(data.efairyreport_title == nil || [data.efairyreport_title isEqualToString:@""]){
            _reportTitle.text = @"待编辑";
            _lastReportTitle = @"待编辑";
        }else{
            _reportTitle.text = data.efairyreport_title;
            _lastReportTitle = data.efairyreport_title;
        }
        
        NSString *alarm = data.efairyreport_project_statistics.total_alarm_device_rows;
        NSString *warning = data.efairyreport_project_statistics.total_early_warning_device_rows;
        NSString *trouble = data.efairyreport_project_statistics.total_trouble_device_rows;
        NSString *deviceRows = data.efairyreport_project_statistics.total_device_rows;
        if(!alarm){
            alarm = @"0";
        }
        if(!warning){
            warning = @"0";
        }
        if(!trouble){
            trouble = @"0";
        }
        if(!deviceRows){
            deviceRows = @"0";
        }
        _reportTime.text = [NSString stringWithFormat:@"%@~%@",data.efairyreport_starttime,data.efairyreport_endtime];
        
        //设置富文本 “/”左边颜色设置
        NSString *alarmTexts= [NSString stringWithFormat:@"%@/%@",alarm,deviceRows];
        NSMutableAttributedString *alarmText = [[NSMutableAttributedString alloc] initWithString:alarmTexts];
        [alarmText addAttribute:NSForegroundColorAttributeName value:ColorFromRGB(0xFF0000) range:[alarmTexts rangeOfString:alarm]];
        [alarmText addAttribute:NSForegroundColorAttributeName value:ColorFromRGB(0x2A2A2A) range:[alarmTexts rangeOfString:deviceRows]];
        _alarmRows.attributedText = alarmText;//报警
        
        
        NSString *warningTexts= [NSString stringWithFormat:@"%@/%@",warning,deviceRows];
        NSMutableAttributedString *warningText = [[NSMutableAttributedString alloc] initWithString:warningTexts];
        [warningText addAttribute:NSForegroundColorAttributeName value:ColorFromRGB(0xFF7200) range:[warningTexts rangeOfString:warning]];
        [warningText addAttribute:NSForegroundColorAttributeName value:ColorFromRGB(0x2A2A2A) range:[warningTexts rangeOfString:deviceRows]];
        _warningRows.attributedText = warningText;//预警
        
        
        NSString *troubleTexts= [NSString stringWithFormat:@"%@/%@",trouble,deviceRows];
        NSMutableAttributedString *troubleText = [[NSMutableAttributedString alloc] initWithString:troubleTexts];
        [troubleText addAttribute:NSForegroundColorAttributeName value:ColorFromRGB(0xFFB900) range:[troubleTexts rangeOfString:trouble]];
        [troubleText addAttribute:NSForegroundColorAttributeName value:ColorFromRGB(0x2A2A2A) range:[troubleTexts rangeOfString:deviceRows]];
        _troubleRows.attributedText = troubleText;//故障
        _projectRows.text = data.efairyreport_project_statistics.total_project_rows;//项目总数
        _score.text = [NSString stringWithFormat:@"最高分%@  平均分%@  最低分%@",
                       data.efairyreport_score_statistics.highest_score,
                       data.efairyreport_score_statistics.average_score,
                       data.efairyreport_score_statistics.lowest_score];//评分
       
    }
    
}

@end
